﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.Tools;
using Halcon二次开发.View.膨胀与腐蚀;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.膨胀或腐蚀
{
    public class SwellEtchController
    {
        string path ;
        //界面参数
        public List<int> parameter = new List<int>()
        {
            0,0
        };
        public HObject ho_GrayImage, ho_Regions1, ho_ConnectedRegions1,ho_RegionErosion, ho_RegionDilation,ho_RegionOpen, ho_RegionClosing;
        public HwindowFlowNodeUrseControl hSmartWindow;
        public int uiTrackBar1, uiTrackBar2, uiTrackBar3, uiTrackBar4, uiTrackBar5, uiTrackBar6, uiTrackBar7, uiTrackBar8, uiTrackBar9, uiTrackBar10, uiTrackBar11, uiTrackBar12, uiTrackBar13, uiTrackBar14;
        //输入图像
        public HObject IntputImage;
        //输出图像(输出一个二值化之后的矩形区域)
        public HObject OutputImage;
        /// <summary>
        /// 预处理窗体对象
        /// </summary>
        private SwellEtchToolFrom form;
        bool paper = false;
        public SwellEtchController(string path, SwellEtchToolFrom form)
        {
            this.form = form;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_Regions1);
             HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_RegionOpen);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            hSmartWindow =StaicResouce.HwindowFlowNodeUrse;
            this.path = path;
        }
        public void Run()
        {
            if (!paper)
            {
                GetModelPaths(form.Name);
                paper = true;
            }
            if (IntputImage == null || ImageIsNg())
            {
                throw new Exception("当前图像为空!");
            }
          
            try
            {

                if (parameter[0] == 0)//二值化
                {
                    Binaryzation(form.uiTrackBar1.Value,form.uiTrackBar2.Value);
                }
                else if (parameter[0] == 1)//膨胀
                {
                    if (parameter[1]==0)
                    {
                       Etch(form.uiTrackBar3.Value,form.uiTrackBar4.Value);
                    }
                    else
                    {
                        Etch(form.uiTrackBar5.Value);
                    }

                }
                else if (parameter[0] == 2)//腐蚀
                {
                    if (parameter[1] ==0)
                    {
                        Swell(form.uiTrackBar7.Value,form.uiTrackBar8.Value);
                    }
                    else
                    {
                        Swell(form.uiTrackBar6.Value);
                    }

                }
                else if (parameter[0] == 3)//开运算
                {
                    if (parameter[1] == 0)
                    {
                       Opening(form.uiTrackBar10.Value,form.uiTrackBar11.Value);
                    }
                    else
                    {
                        Opening(form.uiTrackBar9.Value);
                    }

                }
                else if (parameter[0] == 4)//闭运算
                {
                    if (parameter[1] == 0)
                    {
                        Closing(form.uiTrackBar13.Value,form.uiTrackBar14.Value);
                    }
                    else
                    {
                        Closing(form.uiTrackBar12.Value); ;
                    }

                }
            }
            catch
            {
                throw new Exception("图像预处理失败!");
            }
        }
        /// <summary>
        /// 二值化
        /// </summary>
        /// <param name="floor">灰度值的下限</param>
        /// <param name="upperLimit">灰度值的上限</param>
        public void Binaryzation(int floor, int upperLimit)
        {
            //清除窗口
            HOperatorSet.ClearWindow(StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1.HalconWindow);
            //**************二。通过形态学操作筛选感兴趣区域**********
            //转化灰度图
            HOperatorSet.SetColor(StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1.HalconWindow, "red");
            HOperatorSet.Rgb1ToGray(IntputImage, out ho_GrayImage);
            HOperatorSet.Threshold(ho_GrayImage, out ho_Regions1, floor, upperLimit);
            HOperatorSet.Connection(ho_Regions1, out ho_ConnectedRegions1);
            Dis_Region(ho_Regions1);
        }
        /// <summary>
        /// 腐蚀-矩形
        /// </summary>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public void Swell(int width, int height)
        {
            if (width != 0 && height != 0)
            {
                HOperatorSet.ErosionRectangle1(IntputImage, out ho_RegionErosion, height,
     height);
                Dis_Region(ho_RegionDilation);
            }
            else
            {

            }
        }
        /// <summary>
        /// 腐蚀-圆
        /// </summary>
        /// <param name="radius"></param>
        public void Swell(int radius)
        {
            if (radius != 0)
            {
                HOperatorSet.ErosionCircle(IntputImage, out ho_RegionErosion, radius);
                Dis_Region(ho_RegionDilation);
            }
            else
            {

            }
        }
        /// <summary>
        /// 膨胀-矩形
        /// </summary>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public void Etch(int width, int height)
        {
            if (width != 0 && height != 0)
            {
                HOperatorSet.DilationRectangle1(IntputImage, out ho_RegionDilation, width,
       height);
                Dis_Region(ho_RegionDilation);
            }
            else
            {

            }
        }
        /// <summary>
        /// 膨胀-圆
        /// </summary>
        /// <param name="radius"></param>
        public void Etch(int radius)
        {
            if (radius != 0)
            {
                HOperatorSet.DilationCircle(IntputImage, out ho_RegionDilation, radius);
                Dis_Region(ho_RegionDilation);
            }
            else
            {

            }
        }
        /// <summary>
        ///  开运算-矩形
        /// opening_rectangle1(Regions, RegionOpening, 宽度,高度) 先腐蚀后膨胀 去杂点, 断开等。。。
        /// </summary>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        //开运算
        public void Opening(int width, int height)
        {
            if (width != 0 && height != 0)
            {
                HOperatorSet.OpeningRectangle1(IntputImage, out ho_RegionOpen, width, height);
                Dis_Region(ho_RegionOpen);
            }
            else
            {

            }
        }
        /// <summary>
        ///  开运算-圆
        /// opening_rectangle1(Regions, RegionOpening, 宽度,高度) 先腐蚀后膨胀 去杂点, 断开等。。。
        /// </summary>
        /// <param name="radius">半径</param>
        //开运算
        public void Opening(int radius)
        {
            if (radius != 0)
            {
                HOperatorSet.OpeningCircle(IntputImage, out ho_RegionOpen, radius);
                Dis_Region(ho_RegionOpen);
            }
            else
            {

            }
        }
        /// <summary>
        /// 4 闭运算-矩形
        /// closing_rectangle1(RegionOpening, RegionClosing, 宽度, 高度) 先膨胀后腐蚀 连接, 填充等。。。
        /// </summary>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        //闭运算
        public void Closing(int width, int height)
        {
            if (width != 0 && width != 0)
            {
                HOperatorSet.ClosingRectangle1(IntputImage, out ho_RegionClosing, width, height);
                Dis_Region(ho_RegionClosing);
            }
            else
            {

            }
        }
        /// <summary>
        /// 4 闭运算-圆形
        /// closing_rectangle1(RegionOpening, RegionClosing, 宽度, 高度) 先膨胀后腐蚀 连接, 填充等。。。
        /// </summary>
        /// <param name="radius">半径</param>
        //闭运算
        public void Closing(int radius)
        {
            if (radius != 0)
            {
                HOperatorSet.ClosingCircle(IntputImage, out ho_RegionClosing, radius);
                Dis_Region(ho_RegionClosing);
            }
            else
            {

            }
        }
        //用来显示方法
        public void Dis_Region(HObject region)
        {
            HOperatorSet.DispObj(region, StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1.HalconWindow);
            OutputImage = region;
        }
        public bool ImageIsNg()
        {
            foreach (TreeNode node in StaicResouce.HwindowFlowNodeUrse.treeView1.Nodes)
            {
                if (node.Nodes.Count == 0) continue;
                if (node.Nodes[0].Tag == this && node.Nodes[0].Text.Contains("<—"))
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 设置模式
        /// </summary>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        public void SetParameter(int value1,bool value2)
        {
            parameter[0] = value1;
            if (value2)
            {
                parameter[1] = 0;
            }else
            {
                parameter[1] = 1;
            }
        }
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="path"></param>
        /// <param name="addres"></param>
        public void GetModelPaths(string name)
        {
            string paths = path + "\\" + name + ".json";
            //调用读取方法
            List<int> list  = HelpJsons.Read<int>(paths);

            if (list!=null)
            {
                parameter = list;
            }
        }
        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="path">工程文件名</param>
        /// <param name="addres">节点名</param>
        public void SetModelPaths()
        {
            string paths = path + "\\" + this.form.Name + ".json";
            if (!File.Exists(paths))
            {
                using (File.Create(paths)) { }
            }
            //调用存储方法
         HelpJsons.Write<int>(parameter, paths);
        }
    }
}
